package array;

import java.util.*;

import static java.lang.Math.max;
import static java.lang.Math.min;

public class J20210815 {
    //https://leetcode-cn.com/problems/two-sum/
    public int[] twoSum(int[] nums, int target) {
        int[] B = new int[2];

        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    B[0] = i;
                    B[1] = j;
                    return B;
                }
            }
        }
        return B;
    }

    public int[] twoSum2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{i, map.get(target - nums[i])};
            }
            map.put(nums[i], i);
        }

        return new int[]{0, 1};
    }

    //https://leetcode-cn.com/problems/container-with-most-water/
    public int maxArea(int[] height) {
        int start = 0;
        int end = height.length - 1;
        int area = min(height[start], height[end]) * (end - start);

        while (true) {
            if (height[start] <= height[end]) {
                start++;
            } else {
                end--;
            }

            if (start == end) {
                break;
            }

            area = max(min(height[start], height[end]) * (end - start), area);
        }

        return area;
    }
}
